package com.Nuyoah.demo01.mappers;

import com.Nuyoah.demo01.DTO.RoleBindDTO;
import com.Nuyoah.demo01.DTO.UserDTO;
import com.Nuyoah.demo01.DTO.UserPageQry;
import com.Nuyoah.demo01.VO.UserVO;
import com.Nuyoah.demo01.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;


public interface UserMapper extends BaseMapper<UserDTO> {

    /**
     * 根据用户名查询用户
     * @param username 用户名
     * @return 用户信息
     */
    @Select("SELECT * FROM users WHERE username = #{username}")
    User getUserByName(String username);

    /**
     * 插入用户
     * @param user 用户信息
     * @return 插入成功返回用户ID，失败返回null
     */
    @Insert("INSERT INTO users (username, password, email, created_at, updated_at, status) VALUES (#{username}, #{password}, #{email}, #{createdAt}, #{updatedAt}, #{status})")
    @Options(useGeneratedKeys = true, keyProperty = "userId")
    Integer insertUser(User user);

    /**
     * 查询所有用户
     * @return 用户列表
     */
    @Select("SELECT * FROM users")
    List<User> selectAllUsers();

    /**
     * 分页查询用户
     * @return 用户列表
     */
//    @Select("SELECT * FROM users")
    List<User> selectPageUsers(UserPageQry qry);


    /**
     * 根据用户名和密码查询用户
     * @param username 用户名
     * @param password 密码
     * @return 用户信息
     */
    @Select("SELECT * FROM users WHERE username = #{username} AND password = #{password} LIMIT 1")
    User getUserByUsernameAndPassword(@Param("username") String username, @Param("password") String password);


    //通过用户名查询出用户密码
    @Select("SELECT password FROM users WHERE username = #{username}")
    String getPasswordByUsername(String username);

    /**
     * 更新用户信息
     * @param user 用户信息
     * @return 更新成功返回1，失败返回0
     */
    @Update("UPDATE users SET username = #{username},password = #{password}, email = #{email}, status = #{status}, updated_at = NOW() WHERE user_id = #{userId}")
    Integer updateUser(User user);

    /**
     * 删除用户
     * @param user_id 用户ID
     * @return 删除成功返回1，失败返回0
     */
    @Delete("DELETE FROM users WHERE user_id = #{user_id}")
    Integer deleteUser(Integer user_id);

    /**
     * 封禁用户
     * @param user_id 用户ID
     * @return 封禁成功返回1，失败返回0
     */
    @Update("UPDATE users SET status = 1 WHERE user_id = #{user_id}")
    Integer banUser(Integer user_id);

    /**
     * 解除用户封禁
     * @param user_id 用户ID
     * @return 解封成功返回1，失败返回0
     */
    @Update("UPDATE users SET status = 0 WHERE user_id = #{user_id}")
    Integer unbanUser(Integer user_id);

    /**
     * 根据邮箱查询用户
     * @param email 邮箱
     * @return 用户信息
     */
    @Select("SELECT * FROM users WHERE email = #{email}")
    User getUserByEmail(String email);

    @Select("SELECT * FROM users WHERE user_id = #{userId} ORDER BY created_at DESC")
    UserVO getUserById(Integer userId);

    /*
    * 根据ID查询用户name
     */
    @Select("SELECT username FROM users WHERE user_id = #{userId}")
    User selectByUserId(String userId);

    Integer isAdmin(Integer id);

    Integer setAdmin(Integer id);

    Integer getRoleById(String userId, String roleId);

    Integer bindRole(RoleBindDTO roleBindDTO);

    Integer assignRole(Integer roleId, Integer permId);
    /**
     * 根据ID查询用户(使用MyBatis-Plus的BaseMapper默认实现)
     * @param id 用户ID
     * @return 用户信息
     */
    // 注意：selectById方法已由BaseMapper提供，无需额外定义
}